2.2 服务提供者

首先说明一下,为了便于讲解,本节之后,如无特殊说明,均是以单点的Eureka进行讲解的。

服务提供者和服务消费者

下面这张表格,简单描述了服务提供者/消费者是什么:

名词 概念
服务提供者 服务的被调用方(即:为其他服务提供服务的服务)
服务消费者 服务的调用方(即:依赖其他服务的服务)

服务提供者代码示例

这是一个稍微有点复杂的程序。我们使用spring-data-jpa操作h2数据库,同时将该服务注册到注册中心Eureka中。

  • 创建一个Maven工程,并在pom.xml中添加如下内容:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <artifactId>microservice-provider-user</artifactId>
  6. <packaging>jar</packaging>
  7. <parent>
  8. <groupId>com.itmuch.cloud</groupId>
  9. <artifactId>spring-cloud-microservice-study</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <dependencies>
  13. <!-- 添加Eureka的依赖 -->
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-starter-eureka</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-data-jpa</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.h2database</groupId>
  24. <artifactId>h2</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-actuator</artifactId>
  29. </dependency>
  30. </dependencies>
  31. </project>
  • 配置文件:application.yml
  1. server:
  2. port: 8000
  3. spring:
  4. application:
  5. name: microservice-provider-user # 项目名称尽量用小写
  6. jpa:
  7. generate-ddl: false
  8. show-sql: true
  9. hibernate:
  10. ddl-auto: none
  11. datasource: # 指定数据源
  12. platform: h2 # 指定数据源类型
  13. schema: classpath:schema.sql # 指定h2数据库的建表脚本
  14. data: classpath:data.sql # 指定h2数据库的insert脚本
  15. logging:
  16. level:
  17. root: INFO
  18. org.hibernate: INFO
  19. org.hibernate.type.descriptor.sql.BasicBinder: TRACE
  20. org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
  21. com.itmuch.youran.persistence: ERROR
  22. eureka:
  23. client:
  24. serviceUrl:
  25. defaultZone: http://discovery:8761/eureka/ # 指定注册中心的地址
  26. instance:
  27. preferIpAddress: true
  • 建表语句:schema.sql
  1. drop table user if exists;
  2. create table user (id bigint generated by default as identity, username varchar(255), age int, primary key (id));
  • 插库语句:data.sql
  1. insert into user (id, username, age) values (1,'Tom',12);
  2. insert into user (id, username, age) values (2,'Jerry', 23);
  3. insert into user (id, username, age) values (3,'Reno', 44);
  4. insert into user (id, username, age) values (4,'Josh', 55);
  • DAO:
  1. @Repository
  2. public interface UserRepository extends JpaRepository<User, Long> {
  3. }
  • Controller:
  1. /**
  2. * 作用:
  3. * ① 测试服务实例的相关内容
  4. * ② 为后来的服务做提供
  5. * @author eacdy
  6. */
  7. @RestController
  8. public class UserController {
  9. @Autowired
  10. private DiscoveryClient discoveryClient;
  11. @Autowired
  12. private UserRepository userRepository;
  13. /**
  14. * 注:@GetMapping("/{id}")是spring 4.3的新注解等价于:
  15. * @RequestMapping(value = "/id", method = RequestMethod.GET)
  16. * 类似的注解还有@PostMapping等等
  17. * @param id
  18. * @return user信息
  19. */
  20. @GetMapping("/{id}")
  21. public User findById(@PathVariable Long id) {
  22. User findOne = this.userRepository.findOne(id);
  23. return findOne;
  24. }
  25. /**
  26. * 本地服务实例的信息
  27. * @return
  28. */
  29. @GetMapping("/instance-info")
  30. public ServiceInstance showInfo() {
  31. ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
  32. return localServiceInstance;
  33. }
  34. }
  • 实体类:
  1. @Entity
  2. public class User {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.AUTO)
  5. private Long id;
  6. @Column
  7. private String username;
  8. @Column
  9. private Integer age;
  10. ...
  11. // getters and setters
  12. }
  • 编写Spring Boot启动程序,通过@EnableDiscoveryClient注解,即可将microservice-provider-user服务注册到Eureka上面去
  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class UserApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(UserApplication.class, args);
  6. }
  7. }

至此,代码编写完成。

测试

我们依次启动Eureka服务和microservice-provider-user服务。

访问:http://localhost:8761,如下图。我们会发现microservice-provider-user服务已经被注册到了Eureka上面了。

user-service服务注册到Eureka上

访问:http://localhost:8000/instance-info,返回结果:

  1. {
  2. "host": "192.168.0.59",
  3. "port": 8000,
  4. "metadata": {},
  5. "uri": "http://192.168.0.59:8000",
  6. "secure": false,
  7. "serviceId": "microservice-provider-user"
  8. }

访问:http://discovery:8000/1,返回结果:

  1. {
  2. "id": 1,
  3. "username": "Tom",
  4. "age": 12
  5. }

服务注册到高可用Eureka

如果Eureka 是高可用的,那么各个微服务配置只需要将defaultZone 改为如下即可:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka

代码地址(任选其一)

http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-provider-user
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-provider-user